﻿package Modules
{
	import flash.display.MovieClip;

	public class Utilities
	{
		//Universal utility functions that everyone wants =D
		
		//rounding tools for gridding, factor of 6
		//the rounding factor must be an even number or else the tiles will overlap by 1 factor size
		public static function getNearestGridX(raw:Number):Number{
			
			var remainder:Number = raw%17.5;
			
			if(remainder <= 8.75){
				raw -= remainder;
			}else{
				raw += 17.5-remainder;
			}
			
			return raw;
		}
		
		public static function getNearestGridY(raw:Number):Number{
			
			var remainder:Number = raw%10.5;
			
			if(remainder <= 5.25){
				raw -= remainder;
			}else{
				raw += 10.5-remainder;
			}
			
			return raw;
		}
		
		public static function stripSpaces(originalstring:String):String
		{
			var original:Array=originalstring.split(" ");
			return(original.join(""));
		}
		
		public static function getDaysInMonth(month:uint, year:int = -1):uint{
			if(year == -1)
				year = new Date().fullYear;
			
			if(month == 11)
				return new Date(year, 0, 0).date;
			
			return new Date(year, month + 1, 0).date;
		}
		
		public static function convertMonthToString(month:uint):String{
			switch(month){
				case 0:
					return "Jan";
				case 1:
					return "Feb";
				case 2:
					return "Mar";
				case 3:
					return "Apr";
				case 4:
					return "May";
				case 5:
					return "Jun";
				case 6:
					return "Jul";
				case 7:
					return "Aug";
				case 8:
					return "Sep";
				case 9:
					return "Oct";
				case 10:
					return "Nov";
				case 11:
					return "Dec";
				default:
					return "";
			}
		}
		
		public static function convertMonthToInt(month:String):int{
			switch(month){
				case "Jan":
					return 0;
				case "Feb":
					return 1;
				case "Mar":
					return 2;
				case "Apr":
					return 3;
				case "May":
					return 4;
				case "Jun":
					return 5;
				case "Jul":
					return 6;
				case "Aug":
					return 7;
				case "Sep":
					return 8;
				case "Oct":
					return 9;
				case "Nov":
					return 10;
				case "Dec":
					return 11;
				default:
					return 0;
			}
		}
		
		public static function trim(s:String):String{
			return s ? s.replace(/^\s+|\s+$/gs, '') : ""; 
		}
		
		public static function getEventDate(stringDate:String):Date{
			var dateArray:Array = stringDate.split(" ");
			dateArray[1] = Utilities.convertMonthToInt(dateArray[1]); // convert month to int
			
			return new Date(dateArray[2], dateArray[1], dateArray[0]);
		}
		
		public static function removeHTML(s:String):String{
			var removeHtmlRegExp:RegExp = new RegExp("<[^<]+?>", "gi");
			var noHTMLString:String = s.replace(removeHtmlRegExp, "");
			
			return trim(noHTMLString.split("&nbsp;").join(""));
		}
	}
}